package com.cheetah.start.common.valid;

import cn.hutool.core.util.StrUtil;
import com.cheetah.start.common.valid.anno.Phone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

public class ValidPhoneValidator implements ConstraintValidator<Phone, String> {


    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StrUtil.isBlank(value)) {
            return false;
        } else {
            if (value.length() != 11) {
                return false;
            } else {
                String pattern = "^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$";
                return Pattern.matches(pattern, value);
            }
        }
    }

    @Override
    public void initialize(Phone constraintAnnotation) {

    }
}
